package com.zhiwang.planet.mvp.p

import android.os.Build
import com.cmcm.cmgame.CmGameSdk
import com.iBookStar.views.YmConfig
import com.zhiwang.planet.aop.net.NetWorkRequired
import com.zhiwang.planet.di.ActivityScope
import com.zhiwang.planet.mvp.v.LoginView
import com.zhiwang.planet.mvp.v.LogoutView
import com.zhiwang.planet.mvvm.model.RestApi
import com.zhiwang.planet.mvvm.model.entity.UserDao
import com.zhiwang.planet.mvvm.model.request.LoginRequest
import com.zhiwang.planet.mvvm.model.request.LogoutRequest
import com.zhiwang.planet.util.TokenManager
import com.zhiwang.planet.util.androidSchedule
import com.zhiwang.planet.util.responseFunc
import com.zhiwang.planet.util.responseVoidFunc
import com.tencent.bugly.crashreport.CrashReport
import com.zhiwang.planet.BuildConfig
import javax.inject.Inject

@ActivityScope
class LoginLogoutPresenter @Inject constructor(var restApi: RestApi, var userDao: UserDao) {

    lateinit var loginView: LoginView

    lateinit var logoutView: LogoutView


    @NetWorkRequired
    fun login(phone: String, pwd: String, deviceId: String, wifiName: String) {/*
        var disposable = restApi.login(
                LoginRequest(phone = phone, pwd = pwd, imei = deviceId, get_token = 1,
                        androidVersion = Build.VERSION.RELEASE,
                        appVersionName = BuildConfig.VERSION_NAME,
                        wifiName = wifiName,
                        deviceModel = Build.MODEL))
                .androidSchedule()
                .responseFunc()
                .subscribe({
                    it.avatar = com.zhiwang.planet.util.Utils.fixAvatarUrlPrefix(it.avatar)
                    userDao.deleteAll()
                    userDao.addUser(user = it)
                    TokenManager.saveToken(it.token)
                    TokenManager.saveUserid(it.userId)
                    TokenManager.savePhone(it.phone)
                    //每次登陆切换，都设置异常上报的 userid
                    YmConfig.setOutUserId(it.userId)
                    CrashReport.setUserId(TokenManager.webviewHeaderUserid())
                    TokenManager.saveDevice(deviceId)
                    loginView.onLogin(user = it)
                }, {
                    loginView.onError(it)
                }, {

                })*/
    }


    @NetWorkRequired
    fun logout() {
        var disposable = restApi.logout(LogoutRequest())
                .androidSchedule()
                .responseVoidFunc()
                .subscribe({

                }
                        , {
                    logoutView.onError(it)
                }, {
//                    userDao.deleteAll()
                    TokenManager.clearToken()
                    CmGameSdk.clearCmGameAccount()
                    logoutView.onLogout()
                })
    }

}